﻿using AutoMapper;
using crm_data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace crm_server
{
    public class CustomerService : ICustomerService
    {
        //用户管理仓储接口
        private readonly ICustomerRepository _customerReporitory;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="userRepository">仓储对象</param>
        public CustomerService(ICustomerRepository customerReporitory)
        {
            _customerReporitory = customerReporitory;
        }

        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <returns></returns>
        public List<Customer> GetAllList()
        {
            return _customerReporitory.GetAllList();
        }

        public List<Customer> GetListPage(string customerName, string[] order, int startPage, int pageSize, out int rowCount)
        {
            return _customerReporitory.LoadPageList(startPage, pageSize, out rowCount, c => c.RealName.Contains(customerName), c => c.CreateTime).ToList();
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Customer Get(Guid id)
        {
            return _customerReporitory.FirstOrDefault(c => c.Id == id);
        }

        /// <summary>
        /// 新增客户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Insert(Customer entity)
        {
            var customer = _customerReporitory.InsertOrUpdate(entity);
            return customer == null ? false : true;
        }
        /// <summary>
        ///  修改客户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(Customer entity)
        {
            var customer = _customerReporitory.Update(entity); ;
            return customer == null ? false : true;
        }
    }
}
